using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Xeralabs.GDESK.Test.GDESK;
using Xeralabs.GDESK.Test.Entities;
using Xeralabs.GDESK.Entities;

namespace Xeralabs.GDESK.Test.User
{
    public partial class Test01Form : Form
    {
        private Test01Factory fixedFactory;

        public Test01Form()
        {
            fixedFactory = new Test01Factory();

            InitializeComponent();
        }

        private void EndTestButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddKernelButton_Click(object sender, EventArgs e)
        {
            int index = this.kernelList.Items.Add(new Test01Kernel());
            if (((Test01Kernel)this.kernelList.Items[index]).Initialized)
                index++;
            else
                this.kernelList.Items.RemoveAt(index);
        }
        private void RemoveKernelButton_Click(object sender, EventArgs e)
        {
            if (this.kernelList.SelectedIndex != -1)
            {
                ((Test01Kernel)kernelList.SelectedItem).EndSimulation(false);
                ((Test01Kernel)kernelList.SelectedItem).Dispose();
                kernelList.Items.RemoveAt(kernelList.SelectedIndex);
            }
        }

        private void AddEntityButton_Click(object sender, EventArgs e)
        {
            if (inactiveList.SelectedIndex == -1)
            {
                ((Test01Kernel)kernelList.SelectedItem).ActivateEntity(this.fixedFactory.CreateEntity());
            }
            else
            {
                ((Test01Kernel)kernelList.SelectedItem).ActivateEntity((ulong)inactiveList.SelectedItem);
            }
            UpdateEntityList();
            UpdateInactiveList();
        }
        private void RemoveEntityButton_Click(object sender, EventArgs e)
        {
            if ((kernelList.SelectedIndex != -1) && (entityList.SelectedIndex != -1))
            {
                ((Test01Kernel)kernelList.SelectedItem).DeactivateEntity((ulong)entityList.SelectedItem);
                UpdateEntityList();
                UpdateInactiveList();
            }
        }

        private void KillInactiveButton_Click(object sender, EventArgs e)
        {
            if (inactiveList.SelectedIndex != -1)
            {
                fixedFactory.DisposeInactive((ulong)inactiveList.SelectedItem);
            }
            UpdateEntityList();
            UpdateInactiveList();
        }

        private void KernelList_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (kernelList.SelectedIndex == -1)
            {
                _removeKernelButton.Enabled = false;
                _addEntityButton.Enabled = false;
                _removeEntityButton.Enabled = false;
            }
            else
            {
                _removeKernelButton.Enabled = true;
                _addEntityButton.Enabled = true;
            }
            UpdateEntityList();
            UpdateInactiveList();
        }

        private void EntityList_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (entityList.SelectedIndex == -1)
            {
                _removeEntityButton.Enabled = false;
            }
            else
            {
                _removeEntityButton.Enabled = true;
            }
        }

        private void InactiveList_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (inactiveList.SelectedIndex == -1)
            {
                _killInactiveButton.Enabled = false;
            }
            else
            {
                _killInactiveButton.Enabled = true;
            }
        }

        private void UpdateEntityList()
        {
            int old = entityList.SelectedIndex;
            entityList.Items.Clear();

            if (kernelList.SelectedIndex != -1)
            {
                ulong[] guidArray = ((Test01Kernel)kernelList.Items[kernelList.SelectedIndex]).GetGUIDList();

                for (int a = 0; a < guidArray.Length; a++)
                {
                    entityList.Items.Add(guidArray[a]);
                }

                if (old >= entityList.Items.Count)
                    old = entityList.Items.Count - 1;

                entityList.SelectedIndex = old;
                if (entityList.SelectedIndex == -1)
                    _removeEntityButton.Enabled = false;
            }
        }

        private void UpdateInactiveList()
        {
            int old = inactiveList.SelectedIndex;

            inactiveList.Items.Clear();

            ulong[] guidArray = fixedFactory.GetInactiveList();

            for (int a = 0; a < guidArray.Length; a++)
            {
                inactiveList.Items.Add(guidArray[a]);
            }

            if (old >= inactiveList.Items.Count)
                old = inactiveList.Items.Count - 1;

            inactiveList.SelectedIndex = old;
            if (inactiveList.SelectedIndex == -1)
                _killInactiveButton.Enabled = false;

        }

        protected override void OnClosing(CancelEventArgs e)
        {
            foreach (Test01Kernel kern in this.kernelList.Items)
            {
                ulong[] guids = kern.GetGUIDList();
                if (guids.Length > 0)
                {
                    for (int a = 0; a < guids.Length; a++)
                    {
                        kern.DeactivateEntity(guids[a]);
                        fixedFactory.DisposeInactive(guids[a]);
                    }
                }
                kern.EndSimulation(false);
                kern.Dispose();
            }

            kernelList.Items.Clear();

            this.Owner.Show();

            base.OnClosing(e);
        }
    }
}